

--当魔法恢复满后获得 等同魔法上限的护盾
--护盾上限=取(魔法上限，生命上限)的最大值


local unit_attribute = require 'types.unit_attribute'
local register = unit_attribute.register
register('护盾上限',{
    on_get = function(self,value)
        return math.max(self:get('生命上限'),self:get('护盾强度'))
    end
})

register('护盾强度')

ac.game:event '单位-即将受到治疗'(function(_,unit,source,data)
    local heal = data.heal
    if data:of_reason('治疗') then
        ac.ui_tag {
            string = ('|cff00ff00+%0.f|r'):format(heal),
            speed = 80,
            life = 1,
            fade = 0.5,
            position = unit:get_point(),
        }
    end
end)
--英雄注册护盾
-- ac.game:event '玩家-选择英雄完毕'(function(_,player,hero)
--     hero:loop(20*1000,function()
--         hero:add_buff('魔力护盾'){}
--     end)
-- end)



ac.buff('魔力护盾'){
    physics = true,
    keep = true,
    max_shield = 0, -- 护盾上限
    shield = 0, -- 护盾值
    time = -1,
    on_add = function(self)
        local unit = self.owner 
        self.shield = unit:get('护盾强度')
        self.max_shield = self.shield
        unit:add('护盾',self.shield)
        self:gc(unit:event "单位-伤害护盾计算" (function (_, _, source, damage)
            local value = damage:get_current_damage()
            if value > 0 then 
                local cost = math.min(self.shield, value)
                self.shield = self.shield - cost 
                unit:add('护盾',- cost)
                value = value - cost
                damage:set_current_damage(value)
                if self.shield <= 0 then 
                    -- 护盾破碎的时候触发
                    self:notify("on_finish")
                    self:remove()
                end
                if value <= 0 then 
                    return true
                end
            end 
        end))
        self:gc(function()
            unit:add('护盾',-self.shield)
        end)
    end,
    on_cover = function(new)
        return true
    end
}